package controller;

import java.io.IOException;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShowLinkServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		
		String path = ((HttpServletRequest)request).getRequestURI();
		StringTokenizer st = new StringTokenizer( path,"/");
        int count = st.countTokens(); 

        if(count != 2)
        {
        	try {
				request.getRequestDispatcher("/404error.jsp").forward(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
        	return;
        }
        
		// skip one token /question/abcd (remove sites)
		st.nextToken();
		String title_url = st.nextToken();
		title_url = Utils.fomatDomain(title_url);
		
		request.setAttribute("title_url", title_url);
		
		try {
			request.getRequestDispatcher("/show-link.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
